<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList"
        :span-method="startAndEndTime"
        :row-style="rowStyle"
      >
        <template slot="menuLeft">
          <el-button type="primary" size="small" @click="myAdd">添加</el-button>
          <el-button type="primary" size="small" @click="delBatchs"
            >删除</el-button
          >
          <el-button type="primary" size="small" v-if="false" @click="handleExport"
            >导出</el-button
          >
          <el-button type="primary" size="mini" @click="submitData">加入报表差错</el-button>
        </template>

        <template slot-scope="scope" slot="menu">
          <el-button
            icon="el-icon-plus"
            size="mini"
            type="text"
            @click="myNewAdd(scope.row)"
            v-if="scope.row.submitStatus == '0'"
            >新增配票</el-button
          >
          <el-button
            type="text"
            size="mini"
            icon="el-icon-view"
            @click="getDetail(scope.row)"
            v-if="scope.row.submitStatus == '0'"
            >归还</el-button
          >
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="myEdit(scope.row)"
            v-if="editIsVis(scope.row)"
            >编辑</el-button
          >
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="mergeEdit(scope.row)"
            v-if="editIsViss(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="mini"
            icon="el-icon-view"
            @click="myView(scope.row)"
            >预览</el-button
          >
          <el-button
            icon="el-icon-check"
            type="text"
            size="mini"
            @click="openPastRecords(scope.row)"
            >查看历史记录</el-button
          >
        </template>
      </avue-crud>

      <open-dialog
        :DialogVisible.sync="addVisableDialog"
        title="添加"
        :before-close="beforeAddClose"
        width="80%"
      >
        <el-form v-if="addVisableDialog">
          <div class="header" style="margin-bottom: 10px">基本信息</div>
          <avue-form
            v-model="formAdd.stTicketPettyAdd"
            :option="tableOptionAdd"
          ></avue-form>
          <dynamic-crud
            title="配票明细"
            :data="tableDataDetails"
            :option="tableOptionDetails"
          ></dynamic-crud>
          <div class="bottom-menu">
            <el-button type="primary" @click="submit">确认</el-button>
            <el-button type="primary" @click="save">保存</el-button>
            <el-button type="primary" @click="beforeAddClose"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
      <open-dialog
        :DialogVisible.sync="newAddVisableDialog"
        title="新增配备用金及车票"
        :before-close="beforeAddClose"
        width="80%"
      >
        <el-form v-if="newAddVisableDialog">
          <div class="header" style="margin-bottom: 10px">基本信息</div>
          <avue-form
            v-model="formAdd.stTicketPettyAdd"
            :option="tableOptionNewAdd"
          ></avue-form>
          <dynamic-crud
            title="新增配备用金明细"
            :data="tableCashNewAdd"
            :option="tableOptionCashNewAdd"
            :rowAdd="rowAdd"
          ></dynamic-crud>
          <dynamic-crud
            title="新增配车票明细"
            :data="tableDetailsNewAdd"
            :option="tableOptionDetailsNewAdd"
            :rowAdd="rowAdd"
          ></dynamic-crud>
          <div class="bottom-menu">
            <el-button type="primary" v-if="isMerges == '0'" @click="submit">确认</el-button>
            <el-button type="primary" v-if="isMerges == '1'" @click="confirmObj">确认</el-button>
            <el-button type="primary" @click="saveNew">保存</el-button>
            <el-button type="primary" @click="beforeAddClose"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>

      <open-dialog
        :DialogVisible.sync="editVisableDialog"
        title="编辑"
        :before-close="beforeAddClose"
        width="80%"
      >
        <el-form label-width="60px" v-if="editVisableDialog">
          <avue-form
            v-model="formEdit.stTicketPettyAdd"
            :option="tableOptionAdd"
          ></avue-form>
          <dynamic-crud
            title="配票明细"
            :data="tableDataDetails"
            :option="tableOptionDetails"
          ></dynamic-crud>
          <dynamic-crud
            title="新增配备用金明细"
            :data="tableCashNewAdd"
            :option="tableOptionCashNewAdd"
          ></dynamic-crud>
          <dynamic-crud
            title="新增配车票明细"
            :data="tableDetailsNewAdd"
            :option="tableOptionDetailsNewAdd"
          ></dynamic-crud>
          <div class="bottom-menu">
            <el-button type="primary" @click="editSubmit">确认</el-button>
            <el-button type="primary" @click="edit">保存</el-button>
            <el-button type="primary" @click="beforeAddClose"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
      <open-dialog
        :DialogVisible.sync="mergeEditVisableDialog"
        title="编辑"
        :before-close="beforeAddClose"
        width="80%"
      >
        <el-form label-width="60px" v-if="mergeEditVisableDialog">
          <avue-form
            v-model="formMergeEdit.stTicketPettyAdd"
            :option="tableOptionMergeEdit"
          ></avue-form>
          <dynamic-crud
            title="配票明细"
            :data="tableDataDetails"
            :option="tableOptionDetails"
          ></dynamic-crud>
          <dynamic-crud
            title="新增配备用金明细"
            :data="tableCashNewAdd"
            :option="tableOptionCashNewAdd"
          ></dynamic-crud>
          <dynamic-crud
            title="新增配车票明细"
            :data="tableDetailsNewAdd"
            :option="tableOptionDetailsNewAdd"
          ></dynamic-crud>
          <div class="bottom-menu">
            <el-button type="primary" @click="confirmObj">确认</el-button>
            <el-button type="primary" @click="mergeEditSubmit">保存</el-button>
            <el-button type="primary" @click="beforeAddClose"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>

      <open-dialog
        :DialogVisible.sync="viewVisableDialog"
        title="预览"
        width="80%"
      >
        <div class="header" style="margin-bottom: 10px">基本信息</div>
        <el-form label-width="60px" v-if="viewVisableDialog">
          <avue-form
            v-model="formAdd.stTicketPettyAdd"
            :option="tableOptionView"
          ></avue-form>
          <div class="header" style="margin-top: 10px">配票明细</div>
          <avue-crud
            title="配票明细"
            :data="tableDataDetails"
            :option="tableOptionDetailsView"
          ></avue-crud>
          <div class="header" style="margin-top: 10px">新增配备用金明细</div>
          <avue-crud
            title="新增配备用金明细"
            :data="tableCashNewAdd"
            :option="tableOptionCashNewAdd"
          ></avue-crud>
          <div class="header" style="margin-top: 10px">新增配车票明细</div>
          <avue-crud
            title="新增配车票明细"
            :data="tableDetailsNewAdd"
            :option="tableOptionDetailsNewView"
          ></avue-crud>
          <div class="header" style="margin-top: 10px">归还明细</div>
          <avue-crud
            title="归还明细"
            :data="tableDataReturnDetails"
            :option="tableOptionReturnDetailsView"
          ></avue-crud>
          <div class="bottom-menu">
            <el-button type="primary" @click="viewVisableDialog = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
      <open-dialog title="历史记录" :DialogVisible.sync="visableDialogRecords">
        <el-form label-width="60px">
          <avue-crud
            :data="tableDataRecords"
            :option="tableOptionRecords"
          >
            <template slot="menu" slot-scope="scope">
              <el-button
                icon="el-icon-check"
                :size="scope.size"
                :type="scope.type"
                v-if="scope.row.saveLogFlag=='1'"
                @click="openRecordsDetail(scope.row,scope.index)"
                >查看明细记录</el-button
              >
              <el-button
                icon="el-icon-check"
                :size="scope.size"
                :type="scope.type"
                v-if="scope.row.saveLogFlag=='2'"
                @click="openRecordsDetail(scope.row,scope.index)"
                >查看明细记录</el-button
              >
            </template>
          </avue-crud>
          <div class="bottom-menu">
            <el-button type="primary" @click="visableDialogRecords = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
      <open-dialog
        :DialogVisible.sync="visableDialogRecordsDetail"
        title="历史明细记录"
        width="80%"
      >
        <el-form label-width="60px" v-if="visableDialogRecordsDetail">
          <div class="header" style="margin-top: 10px">配票明细</div>
          <avue-crud
            title="配票明细"
            :data="tableDataDetailsRecords"
            :option="tableOptionDetailsView"
          ></avue-crud>
          <div class="header" style="margin-top: 10px">配备用金明细</div>
          <avue-crud
            title="配备用金明细"
            :data="tableCashNewAddRecords"
            :option="tableOptionCashNewAdd"
          ></avue-crud>
          <!-- <div class="header" style="margin-top: 10px">新增配车票明细</div>
          <avue-crud
            title="新增配车票明细"
            :data="tableDetailsNewAddRecords"
            :option="tableOptionDetailsNewView"
          ></avue-crud> -->
          <div class="bottom-menu">
            <el-button @click="visableDialogRecordsDetail = false"
              >关闭</el-button
            >
          </div>
        </el-form>
      </open-dialog>
      <open-dialog
        :DialogVisible.sync="visableReturnDialogRecordsDetail"
        title="历史归还明细记录"
        width="80%"
      >
        <el-form label-width="60px" v-if="visableReturnDialogRecordsDetail">
          <div class="header" style="margin-top: 10px">归还明细</div>
          <avue-crud
            title="归还明细"
            :data="tableReturnDetailsRecords"
            :option="tableOptionReturnDetailsView"
          ></avue-crud>
          <div class="bottom-menu">
            <el-button @click="visableReturnDialogRecordsDetail = false"
              >关闭</el-button
            >
          </div>
        </el-form>
      </open-dialog>
      <open-dialog
        :DialogVisible.sync="returnVisableDialog"
        title="归还"
        :before-close="beforeAddClose"
        width="80%"
      >
        <el-form v-if="returnVisableDialog">
          <div class="header" style="margin-bottom: 10px">基本信息</div>
          <avue-form
            v-model="formAdd.stTicketPettyAdd"
            :option="tableOptionReturn"
          ></avue-form>
          <dynamic-crud
            title="归还明细"
            :data="tableDataDetails"
            :option="tableOptionDetailsReturn"
            :addDisabled="tableData.length >= 0"
          ></dynamic-crud>
          <div class="bottom-menu">
            <el-button type="primary" v-if="isMerges == '0'" @click="submit">确认</el-button>
            <el-button type="primary" v-if="isMerges == '1'" @click="confirmObj">确认</el-button>
            <el-button type="primary" :disabled="btn" @click="reSave">保存</el-button>
            <el-button type="primary" @click="beforeAddClose"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
      <el-dialog
        title="确认"
        :visible="dialogFormVisible"
        append-to-body
        width="30%"
        :destroy-on-close="true"
      >
        <el-form>
          <el-form-item label="售票员账号">
            <el-input v-model="form1.name" autocomplete="off" style="width:70%"></el-input>
          </el-form-item>
          <el-form-item label="售票员密码">
            <el-input v-model="form1.password" type="password" autocomplete="off" style="width:70%"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
          <el-button size="small" type="primary" @click="confirmUser">确 定</el-button>
        </div>
      </el-dialog>
    </basic-container>
    <addReportEditor ref="addReportEditor" @addReportSuccess="getList"></addReportEditor>
  </div>
</template>

<script>
import baseCrud from "@/mixins/base-crud";
import { dateFormat } from "@/util/date";
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,
  delBatch,
  fetchListDict,
  addcashticketObj,
  fetchListTiketReturn,
  returnObj,
  exportExcel,
  getSwStTicketPettyAdd,
  queryPastRecords,
  queryPastRecordsDetail
} from "@/api/subwaystm/swstticketpetty/swstticketpettyadd";
import {
  tableOption,
  tableOptionAdd,
  tableOptionNewAdd,
  tableOptionView,
  tableOptionReturn,
  tableOptionRecords,
  tableOptionMergeEdit
} from "@/const/crud/subwaystm/swstticketpetty/swstticketpettyadd";
import {
  tableOptionCash,
  tableOptionCashNewAdd,
} from "@/const/crud/subwaystm/swstticketpetty/swstticketpettyaddcashdetails";
import {
  tableOptionDetails,
  tableOptionDetailsNewAdd,tableOptionDetailsNewView,
  tableOptionDetailsReturn,
  tableOptionDetailsView,tableOptionReturnDetailsView
} from "@/const/crud/subwaystm/swstticketpetty/swstticketpettyadddetails";
import { checkPwd,getByUserCode,checkPwdByAccount} from "@/api/admin/user";
import transitUtil from "@/api/subwayRat/transitUtil";
import addReportEditor from '@/views/stationTiecketManager/xianWngCJXXQR/BBCCGG/BBCCGGSQ/Editor';

export default {
  name: "swstticketpettyadd",
  mixins: [baseCrud("subwaystm/swstticketpetty/swstticketpettyadd")],
  components: {
    addReportEditor
  },
  data() {
    return {
      visableDialogRecords: false,
      visableDialogRecordsDetail: false,
      visableReturnDialogRecordsDetail: false,
      tableDataRecords: [],
      tableDataDetailsRecords: [],
      tableReturnDetailsRecords: [],
      tableDataReturnDetails: [],
      tableCashNewAddRecords: [],
      tableDetailsNewAddRecords: [],
      recordsId: [],
      tableOptionRecords: tableOptionRecords,
      tableOptionAdd: tableOptionAdd,
      tableOptionReturn: tableOptionReturn,
      tableOptionNewAdd: tableOptionNewAdd,
      tableOptionView: tableOptionView,
      tableOptionCash: tableOptionCash,
      tableOptionCashNewAdd: tableOptionCashNewAdd,
      tableOptionDetails: tableOptionDetails,
      tableOptionDetailsNewAdd: tableOptionDetailsNewAdd,
      tableOptionDetailsNewView: tableOptionDetailsNewView,
      tableOptionDetailsReturn: tableOptionDetailsReturn,
      tableOptionDetailsView: tableOptionDetailsView,
      tableOptionReturnDetailsView: tableOptionReturnDetailsView,
      addVisableDialog: false,
      newAddVisableDialog: false,
      editVisableDialog: false,
      viewVisableDialog: false,
      returnVisableDialog: false,
      ids: [],
      tableDataDetails: [],
      tableDetailsNewAdd: [],
      tableCashNewAdd: [],
      formAdd: {
        cashDetailsNewAdd: [],
        cashDetailsReturn: [],
        cashDetailsfirst: [],
        stTicketPettyAdd: {},
        ticketDetailsFirst: [],
        ticketDetailsNewAdd: [],
        ticketDetailsReturn: [],
      },
      formEdit: {
        cashDetailsNewAdd: [],
        cashDetailsReturn: [],
        cashDetailsfirst: [],
        stTicketPettyAdd: {},
        ticketDetailsFirst: [],
        ticketDetailsNewAdd: [],
        ticketDetailsReturn: [],
      },
      formMergeEdit: {
        cashDetailsNewAdd: [],
        cashDetailsReturn: [],
        cashDetailsfirst: [],
        stTicketPettyAdd: {},
        ticketDetailsFirst: [],
        ticketDetailsNewAdd: [],
        ticketDetailsReturn: []
      },
      isSubmit: false,
      deviceOperateNum:'',
      searchForm:{
        submitStatus:'0'
      },
      tableOptionMergeEdit: tableOptionMergeEdit,
      mergeEditVisableDialog: false,
      dialogFormVisible: false,
      form1: {},
      saveFlag: false,
      confirmNsJobNo: '',
      isMerges: '0',
      selectKeys:[],
      btn: false
    };
  },
  watch: {},
  created() {
    this.otherVueAction();
  },
  computed: {
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.swstticketpettyadd_add, false),
        delBtn: this.vaildData(this.permissions.swstticketpettyadd_del, false),
        editBtn: this.vaildData(
          this.permissions.swstticketpettyadd_edit,
          false
        ),
      };
    },
  },
  methods: {
    beforeAddClose: function(done) {
      this.$confirm("请确认是否已将所有产生的数据保存", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.addVisableDialog = false;
        this.newAddVisableDialog = false;
        this.editVisableDialog = false;
        this.mergeEditVisableDialog = false;
        this.returnVisableDialog = false;
        done();
      });
    },
    async submitData() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      if (this.$refs.crud.tableSelect.length != 1) {
        this.$message.error("只能选择一条数据");
        return false;
      }
      let workingTime = "";
      let res = false;
      let ress = false;
      for (const table of this.$refs.crud.tableSelect) {
        if (table.submitStatus != "1") {
          this.$message.error("请选择已提交的数据");
          return false;
        }else{
          let now = new Date(table.workingStartTime);
          workingTime = dateFormat(now).substring(0, 10);
          if (table.isAddReprotError == "1") {
            res = await this.$confirm('该数据已加入报表差错，是否重新加入？', '提示', {
              confirmButtonText: '是',
              cancelButtonText: '否',
              type: 'warning'
            });
            if (!res) {
              return;
            }else{
              ress = true;
            }
          }
        }
      }
      if(res==false){
        ress = await this.$confirm("是否加入报表差错", "提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning"
        });
      }
      if (!ress) {
        return;
      }
      this.selectKeys = this.$refs.crud.tableSelect;
      let param={
        dialogTitle: "加入报表差错",
        diffAmountId:this.selectKeys[0].id,
        dataSourceTable:"sw_st_ticket_petty_add",
        lineCode:this.selectKeys[0].lineCode,
        stationCode:this.selectKeys[0].stationCode,
        //bomCode:this.selectKeys[0].bomCode,
        reprotTime:workingTime,
        reportNameId:11,
        deviceNo:this.selectKeys[0].deviceOperateNum,
        settleBeginTime:this.selectKeys[0].workingStartTime,
        settleEndTime:this.selectKeys[0].workingEndTime,
        userCodes:this.selectKeys[0].nsJobNo,
        showType:3,
        dataSource:1,
        page:this.page
      };
      this.$refs.addReportEditor.others_report_add_call(param);
    },
    editIsVis(row) {
      if (row.isMerge == '1') {
        return false;
      } else {
        if (row.submitStatus == 1) {
          return false;
        } else {
          return true;
        }
      }
    },
    editIsViss(row) {
      if (row.isMerge == '0') {
        return false;
      } else {
        if (row.submitStatus == 1) {
          return false;
        } else {
          return true;
        }
      }
    },
    async queryPastRecords(row){
      let param={};
      let listRecords = [];
      param.id=row.id;
      var that = this
      await queryPastRecords(param)
        .then(res => {
          listRecords = res.data.data;
          for( var item of listRecords){
            this.tableDataRecords.push(item.logInfo);
            this.recordsId.push(item.id);
          }
        })
    },
    openPastRecords(row){
      this.tableDataRecords = [];
      this.queryPastRecords(row);
      this.visableDialogRecords=true;
    },
    async queryPastRecordsDetail(param){
      await queryPastRecordsDetail(param)
        .then(res => {
          this.tableDataDetailsRecords = res.data.data.SW_ST_TICKET_PETTY_ADD_DETAILS_0;
          this.tableCashNewAddRecords = res.data.data.SW_ST_TICKET_PETTY_ADD_CASH_DETAILS_0;
          this.tableReturnDetailsRecords = res.data.data.SW_ST_TICKET_PETTY_ADD_DETAILS_2;
        })
    },
    openRecordsDetail(row,index){
      let param={};
      for(var i=0;i<this.recordsId.length;i++){
        if(i==index){
          param.id = this.recordsId[i];
          this.queryPastRecordsDetail(param);
        }
      }
      if(row.saveLogFlag=='1'){
        this.visableDialogRecordsDetail=true;
      }else if(row.saveLogFlag=='2'){
        this.visableReturnDialogRecordsDetail=true;
      }
    },
    delBatchs: function () {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.submitStatus != "0") {
          this.$message.error("请检查数据，只能删除未提交的数据");
          return false;
        }
        if (table.reportModifyStatus == "1") {
          this.$message.error("报表修改状态为“是”的情况下，不允许删除数据");
          return false;
        }
        ids.push(table.id);
      }
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delBatch(ids)
          .then((data) => {
            this.$message.success("删除成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {
            loading();
          });
      });
    },
    getDetail: function (row) {
      this.btn = false;
      this.confirmNsJobNo = row.nsJobNo;
      this.isMerges = '0';
      if(row.isMerge=='1'){
        this.isMerges = '1';
      }
      getObj(row.id).then((response) => {
        let res = response.data.data;
        this.$nextTick(() => {
          let now = new Date();
          if (now.getHours() < 6) {
            now.setDate((now.getDate() - 1));
            this.formAdd.stTicketPettyAdd.workingEndTime = dateFormat(now).substr(0, 10) + " 23:59:59";
          }else{
            this.formAdd.stTicketPettyAdd.workingEndTime = dateFormat(now);
          }
        });
        this.formAdd.stTicketPettyAdd = res.stTicketPettyAdd;
      });
      fetchListTiketReturn(row.id)
        .then((response) => {
          let res = response.data.data;
          for (const item of res) {
            item.$cellEdit = true;
          }
          this.tableDataDetails = res;
        })
        .then(() => {
          this.returnVisableDialog = true;
        });
    },

    edit: function () {
      if (
        this.formEdit.stTicketPettyAdd.nsJobNo == null ||
        this.formEdit.stTicketPettyAdd.nsShift == null ||
        this.formEdit.stTicketPettyAdd.workingStartTime == ""
      ) {
        this.$message.info("售票员、班次、开始时间为必填项，不能为空！");
        return;
      }
      if (this.tableDataDetails.length > 0) {
        for (const item of this.tableDataDetails) {
          if (item.ticketName == null || item.ticketType == null || item.windowsNum == null) {
            this.$message.info("票种、车票名称、开窗张数为必填项！不能为空");
            return;
          }
        }
      }
      if (this.tableCashNewAdd.length > 0) {
        for (const item of this.tableCashNewAdd) {
          if (item.spareAmount == null || item.createTime == null) {
            this.$message.info("配票款金额、操作时间为必填项！不能为空");
            return;
          }
        }
      }
      if (this.tableDetailsNewAdd.length > 0) {
        for (const item of this.tableDetailsNewAdd) {
          if (
            item.ticketType == null ||
            item.windowsNum == null ||
            item.createTime == null
          ) {
            this.$message.info("票种、开窗张数、操作时间为必填项！不能为空");
            return;
          }
        }
      }
      if (!this.isSubmit) {
        this.$message.error("请先输入售票员密码");
        return;
      }
      this.formEdit.ticketDetailsFirst = this.tableDataDetails;
      this.formEdit.cashDetailsfirst = [
        {
          spareAmount: this.formEdit.stTicketPettyAdd.spareAmount,
        },
      ];
      this.formEdit.stTicketPettyAdd.spareAmount = null;
      this.formEdit.cashDetailsNewAdd = this.tableCashNewAdd;
      this.formEdit.ticketDetailsNewAdd = this.tableDetailsNewAdd;
      putObj(this.formEdit)
        .then((data) => {
          this.$message.success("修改成功");
          this.getList(this.page);
          this.editVisableDialog = false;
          this.isSubmit = false;
          done();
        })
        .catch(() => {});
    },
    mergeEditSubmit(){
      if (this.tableDataDetails.length > 0) {
        for (const item of this.tableDataDetails) {
          if (item.ticketName == null || item.ticketType == null || item.windowsNum == null) {
            this.$message.info("票种、车票名称、开窗张数为必填项！不能为空");
            return;
          }
        }
      }
      if (this.tableCashNewAdd.length > 0) {
        for (const item of this.tableCashNewAdd) {
          if (item.spareAmount == null || item.createTime == null) {
            this.$message.info("配票款金额、操作时间为必填项！不能为空");
            return;
          }
        }
      }
      if (this.tableDetailsNewAdd.length > 0) {
        for (const item of this.tableDetailsNewAdd) {
          if (item.ticketType == null||item.windowsNum == null||item.createTime == null) {
            this.$message.info("票种、开窗张数、操作时间为必填项！不能为空");
            return;
          }
        }
      }
      if (!this.saveFlag) {
        this.$message.error("请先输入售票员账号密码确认");
        return;
      }
      this.formMergeEdit.ticketDetailsFirst = this.tableDataDetails;
      this.formMergeEdit.cashDetailsfirst = [
        {
          spareAmount: this.formMergeEdit.stTicketPettyAdd.spareAmount,
        },
      ];
      this.formMergeEdit.stTicketPettyAdd.spareAmount = null;
      this.formMergeEdit.cashDetailsNewAdd = this.tableCashNewAdd;
      this.formMergeEdit.ticketDetailsNewAdd = this.tableDetailsNewAdd;
      putObj(this.formMergeEdit)
        .then((data) => {
          this.$message.success("修改成功");
          this.getList(this.page);
          this.mergeEditVisableDialog = false;
          this.saveFlag = false;
          done();
        })
        .catch(() => {});
    },
    confirmObj(){
      this.form1={};
      this.dialogFormVisible=true;
    },
    mergeEdit(row) {
      this.formMergeEdit = {};
      this.confirmNsJobNo = row.nsJobNo;
      getObj(row.id)
        .then((response) => {
          let res = response.data.data;
          this.tableDataDetails =
            res.ticketDetailsFirst === null ? [] : res.ticketDetailsFirst;
          this.tableDetailsNewAdd =
            res.ticketDetailsNewAdd === null ? [] : res.ticketDetailsNewAdd;
          this.tableCashNewAdd =
            res.cashDetailsNewAdd === null ? [] : res.cashDetailsNewAdd;

          for (const item of this.tableDataDetails) {
            item.$cellEdit = true;
          }
          for (const item of this.tableDetailsNewAdd) {
            item.$cellEdit = true;
          }
          for (const item of this.tableCashNewAdd) {
            item.$cellEdit = true;
          }
          this.formMergeEdit.stTicketPettyAdd = res.stTicketPettyAdd;
          this.formMergeEdit.stTicketPettyAdd.spareAmount = res.cashDetailsfirst[0].spareAmount;
        })
        .then(() => {
          this.mergeEditVisableDialog = true;
        });
    },
    myEdit: function (row) {
      this.formAdd = {};
      getObj(row.id)
        .then((response) => {
          let res = response.data.data;
          this.tableDataDetails =
            res.ticketDetailsFirst === null ? [] : res.ticketDetailsFirst;
          this.tableDetailsNewAdd =
            res.ticketDetailsNewAdd === null ? [] : res.ticketDetailsNewAdd;
          this.tableCashNewAdd =
            res.cashDetailsNewAdd === null ? [] : res.cashDetailsNewAdd;

          for (const item of this.tableDataDetails) {
            item.$cellEdit = true;
          }
          for (const item of this.tableDetailsNewAdd) {
            item.$cellEdit = true;
          }
          for (const item of this.tableCashNewAdd) {
            item.$cellEdit = true;
          }
          this.formEdit.stTicketPettyAdd = res.stTicketPettyAdd;
          this.formEdit.stTicketPettyAdd.spareAmount = res.cashDetailsfirst[0].spareAmount;
        })
        .then(() => {
          this.editVisableDialog = true;
        });
    },
    myView: function (row) {
      this.formAdd = {};
      getObj(row.id)
        .then((response) => {
          let res = response.data.data;
          this.tableDataDetails = res.ticketDetailsFirst;
          this.tableDetailsNewAdd =
            res.ticketDetailsNewAdd === null ? [] : res.ticketDetailsNewAdd;
          this.tableCashNewAdd =
            res.cashDetailsNewAdd === null ? [] : res.cashDetailsNewAdd;
          this.formAdd.stTicketPettyAdd = res.stTicketPettyAdd;
          this.formAdd.stTicketPettyAdd.spareAmount =
            res.cashDetailsfirst[0].spareAmount;
        })
        // .then(() => {
        //   this.viewVisableDialog = true;
        // });
      fetchListTiketReturn(row.id)
        .then((response) => {
          let res = response.data.data;
          this.tableDataReturnDetails = res;
        })
        .then(() => {
          this.viewVisableDialog = true;
        });
    },
    rowStyle({ row, column, rowIndex }) {
      if (row.deviceAttribution == "0") {
        return {
          backgroundColor: "#eee",
          color: "red",
        };
      }
    },
    confirmUser: function () {
      if(this.validatenull(this.form1.name) || this.validatenull(this.form1.password)){
        this.$message.warning("请输入售票员账号和密码！");
        return false;
      }
      if(this.confirmNsJobNo.indexOf(this.form1.name) ==-1){
        this.$message.error("输入的售票员账号不正确!");
        return false;
      }
      checkPwdByAccount(this.form1.name, this.form1.password).then((response) => {
        let data = response.data.data;
        if (data) {
          this.$message.success("确认成功");
          this.dialogFormVisible = false;
          this.saveFlag = true;
          this.isSubmit = true;
        } else {
          this.$message.error("用户名或密码错误！");
        }
      });
    },
    submit() {
      if (this.formAdd.stTicketPettyAdd.nsJobNo == "") {
        this.$message.error("请选择售票员！");
        return;
      }
      this.isSubmit = true;
      this.$prompt("请输入售票员密码", "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputType: "password",
        inputValidator: (val) => {
          if (!val) return "请输入密码";
        },
      })
        .then(({ value }) => {
          return checkPwd(this.formAdd.stTicketPettyAdd.nsJobNo, value);
        })
        .then((response) => {
          var result = response.data.data;
          if (result) {
            this.$message.success("确认成功");
            this.isSubmit = true;
            return;
          } else {
            this.$message.error("密码错误");
            return Promise.reject("密码错误");
          }
        })
        .catch((error) => {
          this.isSubmit = false;
        });
    },
    editSubmit() {
      if (this.formEdit.stTicketPettyAdd.nsJobNo == "") {
        this.$message.error("请选择售票员！");
        return;
      }
      this.isSubmit = true;
      this.$prompt("请输入售票员密码", "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputType: "password",
        inputValidator: (val) => {
          if (!val) return "请输入密码";
        },
      })
        .then(({ value }) => {
          return checkPwd(this.formEdit.stTicketPettyAdd.nsJobNo, value);
        })
        .then((response) => {
          var result = response.data.data;
          if (result) {
            this.$message.success("确认成功");
            this.isSubmit = true;
            return;
          } else {
            this.$message.error("密码错误");
            return Promise.reject("密码错误");
          }
        })
        .catch((error) => {
          this.isSubmit = false;
        });
    },
    startAndEndTime({ row, column, rowIndex, columnIndex }) {
      row.startAndEnd =
        row.workingStartTime +
        (row.workingEndTime != null ? "~" + row.workingEndTime : "");
    },
    async getUserDeviceOperateNo(parames){
      await getByUserCode(parames)
        .then((response) => {
          this.deviceOperateNum = response.data.data.deviceOperateNo;
        })
    },
    save: function () {
      this.formAdd.ticketDetailsFirst = this.tableDataDetails;
      this.formAdd.cashDetailsfirst = [
        {
          spareAmount: this.formAdd.stTicketPettyAdd.spareAmount,
        },
      ];
      if (
        this.formAdd.stTicketPettyAdd.nsJobNo == null ||
        this.formAdd.stTicketPettyAdd.nsShift == null ||
        this.formAdd.stTicketPettyAdd.workingStartTime == ""
      ) {
        this.$message.info("售票员、班次、开始时间为必填项，不能为空！");
        return;
      }
      if(this.formAdd.stTicketPettyAdd.spareAmount!==undefined){
        if(String(this.formAdd.stTicketPettyAdd.spareAmount).length>5){
          this.$message.info(
            "请检查配备用金金额不能超过5位数！"
          );
          return;
        }
      }
      if (this.tableDataDetails.length > 0) {
        for (const item of this.tableDataDetails) {
          if (item.ticketName == null || item.ticketType == null || item.windowsNum == null) {
            this.$message.info("票种、车票名称、开窗张数为必填项！不能为空");
            return;
          }
        }
      }
      if (!this.isSubmit) {
        this.$message.error("请先输入售票员密码");
        return;
      }
      this.formAdd.stTicketPettyAdd.spareAmount = null;
      let now = new Date();
      let future = new Date();
      if (now.getHours() < 6) {
        now.setDate(now.getDate() - 1);
      }else if(now.getHours() > 6){
        future.setDate(now.getDate() + 1);
      }
      this.getUserDeviceOperateNo(this.formAdd.stTicketPettyAdd.nsJobNo).then(() => {
        if(this.formAdd.stTicketPettyAdd.deviceOperateNum!=this.deviceOperateNum){
          this.$confirm("请检查所选择的设备操作号与该售票员不匹配，是否确定保存？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            if(
              this.formAdd.stTicketPettyAdd.workingStartTime >= dateFormat(future).substr(0, 10) + " 02:00:00" || 
              this.formAdd.stTicketPettyAdd.workingStartTime < dateFormat(now).substr(0, 10) + " 02:00:00"
            ){
              this.$confirm("开始时间不是系统默认时间,是否保存？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
              }).then(() => {
                addObj(this.formAdd)
                .then((data) => {
                  this.$message.success("添加成功");
                  this.getList(this.page);
                  this.addVisableDialog = false;
                  this.isSubmit = false;
                  done();
                })
                .catch(() => {
                });
              })
            } else {
              addObj(this.formAdd)
              .then((data) => {
                this.$message.success("添加成功");
                this.getList(this.page);
                this.addVisableDialog = false;
                this.isSubmit = false;
                done();
              })
              .catch(() => {
              });
            }
          });
        }else{
          if(
            this.formAdd.stTicketPettyAdd.workingStartTime >= dateFormat(future).substr(0, 10) + " 02:00:00" || 
            this.formAdd.stTicketPettyAdd.workingStartTime < dateFormat(now).substr(0, 10) + " 02:00:00"
          ){
            this.$confirm("开始时间不是系统默认时间,是否保存？", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              addObj(this.formAdd)
              .then((data) => {
                this.$message.success("添加成功");
                this.getList(this.page);
                this.addVisableDialog = false;
                this.isSubmit = false;
                done();
              })
              .catch(() => {
              });
            })
          } else {
            addObj(this.formAdd)
            .then((data) => {
              this.$message.success("添加成功");
              this.getList(this.page);
              this.addVisableDialog = false;
              this.isSubmit = false;
              done();
            })
            .catch(() => {
            });
          }
        }
      });
    },
    reSave: function () {
      if (
        this.formAdd.stTicketPettyAdd.workingEndTime == null ||
        this.formAdd.stTicketPettyAdd.totalHandSpareAmount == null
      ) {
        this.$message.info("归还时间、配备用金金额为必填项，不能为空！");
        return;
      }
      if (
        dateFormat(new Date(this.formAdd.stTicketPettyAdd.workingStartTime)).substr(0, 10) != 
        dateFormat(new Date(this.formAdd.stTicketPettyAdd.workingEndTime)).substr(0, 10)
      ) {
        this.$message.info("归还时间必须和配票开始时间是同一天！");
        return;
      }      
      if (
        dateFormat(new Date(this.formAdd.stTicketPettyAdd.workingStartTime)) >
        dateFormat(new Date(this.formAdd.stTicketPettyAdd.workingEndTime))
      ) {
        this.$message.info("归还时间不能小于配票开始时间！");
        return;
      }
      if (this.tableDataDetails.length > 0) {
        for (const item of this.tableDataDetails) {
          if (
            item.ticketType == null ||
            item.windowsCloseNum == null ||
            item.invalidTicketNum == null
          ) {
            this.$message.info("票种、关窗张数、废票张数为必填项！不能为空");
            return;
          }
          if (
            Number(item.windowsCloseNum)  - Number(item.windowsNum) > 0
          ) {
            this.$message.info("请检查关窗张数不能大于开窗张数！");
            return;
          }
          if (
            Number(item.invalidTicketNum)  - Number(item.windowsNum) > 0
          ) {
            this.$message.info("请检查废票张数不能大于开窗张数！");
            return;
          }
	  if (
            (Number(item.invalidTicketNum)+Number(item.windowsCloseNum)+Number(item.cashSaleNum))  - Number(item.windowsNum) > 0
          ) {
            this.$message.info("请检查现金售卖张数不能大于开窗-关窗-废票张数！");
            return;
          }
        }
      }
      if (!this.isSubmit) {
        this.$message.error("请先输入售票员密码");
        return;
      }
      this.formAdd.ticketDetailsReturn = this.tableDataDetails;
      this.formAdd.cashDetailsReturn = [
        {
          spareAmount: this.formAdd.stTicketPettyAdd.totalHandSpareAmount,
        },
      ];
      this.formAdd.stTicketPettyAdd.totalHandSpareAmount = null;
      this.btn = true;
      returnObj(this.formAdd)
        .then((data) => {
          this.$message.success("归还成功");
          this.getList(this.page);
          this.returnVisableDialog = false;
          this.isSubmit = false;
          done();
        })
        .catch(() => {
          loading();
        });
    },
    saveNew: function () {
      if (
        this.tableCashNewAdd.length == 0 &&
        this.tableDetailsNewAdd.length == 0
      ) {
        this.$message.info("请新增配备用金明细信息/配车票明细信息");
        return;
      }
      if (this.tableCashNewAdd.length > 0) {
        for (const item of this.tableCashNewAdd) {
          if (item.spareAmount == null || item.createTime == null) {
            this.$message.info("配票款金额、操作时间为必填项！不能为空");
            return;
          }
        }
      }
      if (this.tableDetailsNewAdd.length > 0) {
        for (const item of this.tableDetailsNewAdd) {
          if (
            item.ticketType == null ||
            item.windowsNum == null ||
            item.createTime == null
          ) {
            this.$message.info("票种、开窗张数、操作时间为必填项！不能为空");
            return;
          }
        }
      }
      if (!this.isSubmit) {
        this.$message.error("请先输入售票员密码");
        return;
      }
      this.formAdd.ticketDetailsNewAdd = this.tableDetailsNewAdd;
      this.formAdd.cashDetailsNewAdd = this.tableCashNewAdd;
      addcashticketObj(this.formAdd)
        .then((data) => {
          this.$message.success("添加成功");
          this.getList(this.page);
          this.newAddVisableDialog = false;
          done();
        })
        .catch(() => {
          loading();
        });
    },
    myAdd: function () {
      for (var i in this.formAdd.stTicketPettyAdd) {
        this.formAdd.stTicketPettyAdd[i] = null;
      }
      this.tableDataDetails = [];
      this.addVisableDialog = true;
      // this.$nextTick(() => {
      //   let now = new Date();
      //   if (now.getHours() < 6) {
      //     now.setDate(now.getDate() - 1);
      //   }
      //   this.formAdd.stTicketPettyAdd.workingStartTime = dateFormat(now);
      // });
      let now = new Date();
      if (now.getHours() < 6) {
        //now.setDate((now.getDate() - 1));
        this.formAdd.stTicketPettyAdd.workingStartTime = dateFormat(now).substr(0, 10) + " 06:00:00";
      }else{
        this.formAdd.stTicketPettyAdd.workingStartTime = dateFormat(now);
      }
    },
    rowAdd: function (callback) {
      callback({
        createTime: dateFormat(new Date()),
      });
    },
    myNewAdd: function (row) {
      this.formAdd = {};
      this.tableDataDetails = [];
      this.tableDetailsNewAdd = [];
      this.tableCashNewAdd = [];
      this.formAdd.stTicketPettyAdd = row;
      this.confirmNsJobNo = row.nsJobNo;
      this.isMerges = '0';
      if(row.isMerge=='1'){
        this.isMerges = '1';
      }
      this.newAddVisableDialog = true;
    },
    selectionChange(list) {
      this.ids = list.map(function (value, index, array) {
        return value.id;
      });
    },
    handleReview() {
      if (this.ids.length > 0) {
        let params = { ids: this.ids.join(",") };
        reviewSwTicket(params).then((response) => {
          this.$message.success("审阅成功");
          this.getList(this.page);
        });
      } else {
        this.$message.info("您必须选中至少一条数据进行审阅！");
      }
    },
    otherVueAction(){
      transitUtil.$on('updateReport_call_11',(param)=>{
        let p=param;
        // p.lineCode=param.lineCode;
        // p.stationCode=param.stationCode;
        // p.supplementCurrencyTime=param.reprotTime;
        //p.tvmCode=param.tvmCode;
        getSwStTicketPettyAdd(p).then(response => {
          this.tableData = response.data.data;
          // this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.header {
  margin: 0 auto;
  width: 99%;
  padding: 5px 10px;
  border: 1px solid #e4e4e4;
  border-bottom: none;
  background-color: #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.bottom-menu {
  margin-top: 58px;
  text-align: center;
}
</style>
